using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using RAG.Application.Interfaces;
using RAG.Application.Dtos;

namespace RAG.Api.Controllers;

/// <summary>
/// 向量管理控制器
/// </summary>
[ApiController]
[Route("api/vector-management")]
[Authorize]
public class VectorManagementController : ControllerBase
{
    private readonly IAppVectorService _vectorService;

    public VectorManagementController(IAppVectorService vectorService)
    {
        _vectorService = vectorService;
    }

    /// <summary>
    /// 获取向量列表
    /// </summary>
    [HttpGet]
    public async Task<IActionResult> GetVectorList([FromQuery] VectorListQueryDto query)
    {
        try
        {
            var result = await _vectorService.GetVectorListAsync(query);
            return Ok(result);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { code = 500, message = $"获取向量列表时发生错误: {ex.Message}" });
        }
    }

    /// <summary>
    /// 获取向量详情
    /// </summary>
    [HttpGet("{vectorId}")]
    public async Task<IActionResult> GetVectorDetail(Guid vectorId)
    {
        try
        {
            var result = await _vectorService.GetVectorDetailAsync(vectorId);
            
            if (result.Code == 200)
            {
                return Ok(result);
            }
            else
            {
                return BadRequest(result);
            }
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { code = 500, message = $"获取向量详情时发生错误: {ex.Message}" });
        }
    }

    /// <summary>
    /// 生成单个向量
    /// </summary>
    [HttpPost("generate")]
    public async Task<IActionResult> GenerateVector([FromBody] GenerateVectorDto dto)
    {
        try
        {
            var result = await _vectorService.GenerateVectorAsync(dto);
            
            if (result.Code == 200)
            {
                return Ok(result);
            }
            else
            {
                return BadRequest(result);
            }
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { code = 500, message = $"生成向量时发生错误: {ex.Message}" });
        }
    }

    /// <summary>
    /// 批量生成向量
    /// </summary>
    [HttpPost("batch-generate")]
    public async Task<IActionResult> BatchGenerateVectors([FromBody] BatchGenerateVectorDto dto)
    {
        try
        {
            var result = await _vectorService.BatchGenerateVectorsAsync(dto);
            
            if (result.Code == 200)
            {
                return Ok(result);
            }
            else
            {
                return BadRequest(result);
            }
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { code = 500, message = $"批量生成向量时发生错误: {ex.Message}" });
        }
    }

    /// <summary>
    /// 删除向量
    /// </summary>
    [HttpDelete("{vectorId}")]
    public async Task<IActionResult> DeleteVector(Guid vectorId)
    {
        try
        {
            var result = await _vectorService.DeleteVectorAsync(vectorId);
            
            if (result.Code == 200)
            {
                return Ok(result);
            }
            else
            {
                return BadRequest(result);
            }
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { code = 500, message = $"删除向量时发生错误: {ex.Message}" });
        }
    }

    /// <summary>
    /// 向量相似度搜索
    /// </summary>
    [HttpPost("similarity")]
    public async Task<IActionResult> SearchVectorSimilarity([FromBody] VectorSimilaritySearchDto dto)
    {
        try
        {
            var result = await _vectorService.SearchVectorSimilarityAsync(dto);
            return Ok(result);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { code = 500, message = $"向量相似度搜索时发生错误: {ex.Message}" });
        }
    }

    /// <summary>
    /// 向量相似度搜索（仅返回列表，便于前端直接渲染）
    /// </summary>
    [HttpPost("similarity-list")]
    public async Task<IActionResult> SearchVectorSimilarityList([FromBody] VectorSimilaritySearchDto dto)
    {
        try
        {
            var result = await _vectorService.SearchVectorSimilarityListAsync(dto);
            return Ok(result);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { code = 500, message = $"向量相似度搜索时发生错误: {ex.Message}" });
        }
    }

    /// <summary>
    /// 获取向量统计信息
    /// </summary>
    [HttpGet("statistics")]
    public async Task<IActionResult> GetVectorStatistics()
    {
        try
        {
            var result = await _vectorService.GetVectorStatisticsAsync();
            return Ok(result);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { code = 500, message = $"获取向量统计信息时发生错误: {ex.Message}" });
        }
    }
}
